package com.warwithinme.android.widgetwar;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;

public class ButtonGPS extends TButton
{

	private final static String INTENT_ACTION = "com.warwithinme.android.widgetwar.ButtonGPS";
	
	@Override
	public int canHandleIntent(Intent intent)
	{
		if (intent.getAction().equals(INTENT_ACTION))
			return ToggleWidget.HANDLING_TYPE_DONE;
		else
			return ToggleWidget.HANDLING_TYPE_UNABLE;
	}

	@Override
	public int getButtonBmp(Context context)
	{
		
		// 1.5 doesn't have isLocationProviderEnabled();
		// boolean gpsEnabled =
		// Settings.Secure.isLocationProviderEnabled(context.getContentResolver(),
		// LocationManager.GPS_PROVIDER);
		String providers = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
		boolean gpsOn = providers.contains(LocationManager.GPS_PROVIDER);
		if (gpsOn)
			return R.drawable.gps_on;
		else
			return R.drawable.gps_off;
	}

	@Override
	public String getIntentAction()
	{
		return INTENT_ACTION;
	}

	@Override
	public void toggleSetting(Context context)
	{
		Intent intent = new Intent();
		intent.setClassName("com.android.settings", "com.android.settings.LocationSettings");
		PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		try
		{
			pIntent.send();
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
}
